<?php

namespace app\admin\model;

use think\Model;

class Images extends Model
{

    // 定义数据表名
    protected $name = 'images';
    protected $oss_domain_name;
    protected $schema = [
        'id' => 'int',
        'directory' => 'int',
        'url' => 'string',
        'thumb' => 'string',
        'alt' => 'string',
        'is_commit' => 'int',
        'create_time' => 'datetime',
    ];
    // 访问器：拼接固定域名
    public function getUrlAttr($value)
    {
        $domain = 'https://' . config('filesystem.aliyun-oss.bucket') . '.' . config('filesystem.aliyun-oss.endpoint'); // 替换为你的实际域名
        return $domain . $value;
    }

    public function getImagesByIds($imagesIds)
    {

        $data = $this
            ->whereIn('id', $imagesIds)
            ->field('id,url,name,alt')
            ->select()
            ->toArray();
        // foreach ($data as $key => &$value) {
        //     $value['url'] = 'https://' . config('filesystem.aliyun-oss.bucket') . '.' . config('filesystem.aliyun-oss.endpoint') . $value['url'];
        // }

        return $data;
    }

    public function getImageById($id)
    {
        $data = $this
            ->where('id', $id)
            ->field('id,url,name,alt')
            ->find();
        if (!$data) {
            return ['url' => ''];
        }
        // $data['url'] = 'https://' . config('filesystem.aliyun-oss.bucket') . '.' . config('filesystem.aliyun-oss.endpoint') .  $data['url'];
        return $data;
    }
}
